<template>
<el-container>
  <el-header style="background-color: white; color: black;">
      <h1 align="center">角色管理</h1>
  </el-header>
  <el-main>
    <el-button type="primary" @click="addUser()">添加</el-button>
    <el-table :data="tableData" style="100%">
      <!-- 列组件 -->
      <!-- prop 对应当前页集合的对象属性-->
      <el-table-column prop="roleName" label="角色名" width="100"></el-table-column>
      <el-table-column prop="myNodeList" label="权限" style="100%"></el-table-column>
      <el-table-column label="操作" width="200">
          <!-- slot-scope 插槽: 用来获取循环的迭代变量-->
          <template slot-scope="scope">
            <el-button type="danger" size="mini" @click="update(scope.row,scope.$index)">修改</el-button>
            <el-button type="danger" size="mini" @click="deleteRole(scope.row,scope.$index)">删除</el-button>
          </template>
      </el-table-column>
    </el-table>
    <el-dialog :title="title" :visible.sync="isShow" width="50%">
        <el-form :model="roleForm" status-icon  ref="roleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="姓名" prop="roleName">
                <el-input v-model="roleForm.roleName"></el-input>
            </el-form-item>
            <el-form-item label="权限" prop="myNodeList">
                <el-tree
                  :data="roleData"
                  show-checkbox
                  node-key="nodeId"
                  :default-expanded-keys="expandedKeys"
                  :default-checked-keys="checkedKeys"
                  ref="tree"
                  highlight-current
                  :props="defaultProps">
                </el-tree>
            </el-form-item>
            <el-form-item>
                <el-button @click="submitForm(index)">提交</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>
  </el-main>
</el-container>
</template>

<script>
  export default {
      data() {
        return {
          isShow:false,
          tableData: [],
          roleData:[],
          title:"修改",
          index:0 ,  //0为修改  1为添加
          expandedKeys:[],
          checkedKeys:[],
          roleForm: {
            roleId:0,
            roleName:"",
            powerList:[],
          },
          defaultProps: {
            hildren: 'children',
            label: 'name',
            id:'nodeId'
          }
        }
      },
      methods: {
        loadData() { //加载数据
          var self = this;
          var http = this.$http.post("/api/role/selectRolePower",);
          http.then(function(rs) {
            if (rs.data.code == 200) {
              self.tableData = rs.data.data;
              for(var i = 0;i < rs.data.data.length;i++){
                var s = "";
                for(var j = 0;j < rs.data.data[i].myNodeList.length;j++){
                  if(rs.data.data[i].myNodeList[j].parentId != null && rs.data.data[i].myNodeList[j].parentId != 0 ){
                    s += rs.data.data[i].myNodeList[j].name + ",";
                  }
                }
                self.tableData[i].myNodeList = s;
              }

            }else{
              self.tableData=[];
            }
          })
        },
        update(x,index) { //修改回显
          this.index = 0;
          this.title = "修改"
          this.isShow = true;
          this.roleForm = x;
          var self = this;
          var http = this.$http.post("api/role/queryPowerByRole",this.$qs.stringify(this.roleForm));
          http.then(function(rs){
              if(rs.data.code == 200){
                self.expandedKeys=[];
                self.checkedKeys=[];
                for(var i = 0;i < rs.data.data.length;i++){
                  self.expandedKeys.push(rs.data.data[i].nodeId);
                  for(var j = 0;j < rs.data.data[i].children.length;j++){
                    self.checkedKeys.push(rs.data.data[i].children[j].nodeId)
                  }
                }
              }
          })
        },
        addUser(){
          this.roleForm.roleId = 0;
          this.roleForm.roleName = "";
          this.title = "添加"
          this.isShow = true;
          this.index = 1;
        },
        deleteRole(x,index) { //删除
          this.roleForm = x;
          var self = this
          var http = this.$http.post("api/role/deleteRole",this.$qs.stringify(this.roleForm));
          http.then(function(rs){
              if(rs.data.code == 200){
                alert(rs.data.msg)
                location.reload();
              }
          })
        },
        submitForm(i){
          this.getCheckedKeys()
          if(i == 0){
            var self = this
            var http = this.$http.post("api/role/updateRole",this.$qs.stringify(this.roleForm));
            http.then(function(rs){
                if(rs.data.code == 200){
                  alert(rs.data.msg)
                  location.reload();
                }else{
                  alert(rs.data.msg)
                }
            })
          }else{
            var self = this
            var http = this.$http.post("api/role/addRole",this.$qs.stringify(this.roleForm));
            http.then(function(rs){
                if(rs.data.code == 200){
                  alert(rs.data.msg)
                  location.reload();
                }else{
                  alert(rs.data.msg)
                }
            })
          }
        },
        selectPower(){
          var self = this
          var http = this.$http.post("api/role/queryAllPower",);
          http.then(function(rs){
              if(rs.data.code == 200){
                self.roleData = rs.data.data
              }
          })
        },
        getCheckedKeys() {
          this.roleForm.powerList = this.$refs.tree.getCheckedKeys();
        },
      },
      mounted(){
        this.loadData();
        this.selectPower()
      },
    }
</script>

<style>
  .el-table .warning-row {
      background: oldlace;
    }

    .el-table .success-row {
      background: #f0f9eb;
    }
</style>
